package com.next.core.container;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fei.wang
 *
 */
public class ModuleContainer implements IModuleContainer{

	private Map<String, Object> moduleMap;
	
	public ModuleContainer() {
		this.moduleMap = new HashMap<String, Object>();
	}

	public <T> T getModule(Class<T> clazz) {
		return getModule(clazz.getName());
	}
	
	@SuppressWarnings("unchecked")
	public <T> T getModule(String name) {
		return (T) moduleMap.get(name);
	}
	
	public void addModule(String name, Object obj) {
		moduleMap.put(name, obj);
	}

	public <T> boolean addModule(Class<T> clazz) {
		try {
			T obj = clazz.newInstance();
			addModule(clazz.getName(), obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}
}
